// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const wxContext = cloud.getWXContext()
    const openId = wxContext.OPENID
   
    const date = new Date(event.orderTime);
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份从 0 开始，需要加 1
    const day = date.getDate();
    const hours = date.getHours()+8;
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
    const formattedDateTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

    const result = await cloud.openapi.subscribeMessage.send({
      "touser": "orher69g6zXOBoLH_xNXOQJtIopQ",//需要推送给谁
      "page": 'index',
      "lang": 'zh_CN',
      "data": {
        "name2": {
          "value": "李四"
        },
        "thing3":{
          "value":"有客人下单了，快去查看"
        },        
        "time4": {
          "value":formattedDateTime
         },
         "thing6":{
          "value": "岭南师范学院"
         },
         "thing5":{
          "value": "不加冰"
         }
      },
      "templateId": 'i8NZ7V4lfNGbjGmMq_O0ctfInrs-JJ6sQ3B1vO5mUCw'
    })
    return result.errCode
  } catch (err) {
    return err
  }
}